<template>
  <div class="goodsinfo-box">
    <transition
      @before-enter="beforeEnter"
      @enter="enter"
      @after-enter="afterEnter"
    >
      <div id="ball" v-show="flag"></div>
    </transition>
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swiper :test="lunboList"></swiper>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">{{goodsInfo.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div class="price">
            市场价:<span> {{goodsInfo.market_price}}</span>
            销售价:<span> {{goodsInfo.sell_price}}</span>
          </div>
          <div class="count">
            <span>购买数量: &nbsp;</span> <count @give="get" :num="goodsInfo.stock_quantity"/>
          </div>
          <div>
            <mt-button type="primary" size="small">立即购买</mt-button> &nbsp;
            <mt-button type="danger" size="small" @click="goCar">加入购物车</mt-button>
          </div>
        </div>
      </div>
    </div>
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <div>商品编号: {{goodsInfo.goods_no}}</div>
          <div>商品库存: {{goodsInfo.stock_quantity}}</div>
          <div>上架时间: {{goodsInfo.add_time}}</div>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button @click="goInfoDes" type="primary" plain size="large">商品详情</mt-button>
        <mt-button @click="goComment" type="danger" plain size="large">商品评价</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swiper from "../common/swiper.vue";
import count from "../common/count.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      lunboList: [],
      goodsInfo: {},
      flag: false,
      count: 1
    };
  },
  components: {
    swiper,
    count
  },
  created() {
    this.getLunboList();
    this.getGoodsInfo();
  },
  methods: {
    get(data) {
      this.count = parseInt(data);
    },
    getLunboList() {
      this.$http.get("api/getthumimages/" + this.id).then(res => {
        this.lunboList = res.body.message;
      });
    },
    getGoodsInfo() {
      this.$http.get("api/goods/getinfo/" + this.id).then(res => {
        this.goodsInfo = res.body.message[0];
      });
    },
    goInfoDes() {
      this.$router.push({ name: "infoDes", params: { id: this.id } });
    },
    goComment() {
      this.$router.push({ name: "goComment", params: { id: this.id } });
    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;
      let badge = document.getElementById("badge"),
        ball = document.getElementById("ball"),
        x =
          badge.getBoundingClientRect().left -
          ball.getBoundingClientRect().left,
        y =
          badge.getBoundingClientRect().top - ball.getBoundingClientRect().top;
      el.style.transform = `translate(${x}px, ${y}px)`;
      el.style.transition = "all ease .6s";
      done();
    },
    afterEnter(el) {
      this.flag = false;
    },
    goCar() {
      this.flag = true;
    }
  }
};
</script>
<style lang="scss" scoped>
.goodsinfo-box {
  #ball {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    top: 390px;
    left: 150px;
    z-index: 99;
  }
  .count {
    margin: 5px 0;
    display: flex;
    align-items: center;
  }
  .mui-card-header,
  .mui-card-footer {
    display: block;
  }
  .mint-button--primary.is-plain {
    margin: 5px 0;
  }
}
</style>